// components/wx-phonenumber-get.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        multipleSlots: true
    },

    /**
     * 组件的初始数据
     */
    data: {
        phone: "",
        dialog1: false,
        butChck: false,
        userInfo: {},
        hasUserInfo: false,
        canIUseGetUserProfile: true,
        url: getApp().globalData.apiurl,
        loginForm: {
            username: "",
            password: ""
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        //获取手机号
        getPhoneNumber: function (e) {
            //console.log(e)
            let that = this
            if (e.detail.errMsg == undefined || e.detail.errMsg !== "getPhoneNumber:ok") {
                wx.showModal({
                    title: '提示',
                    content: e.detail.errMsg,
                    showCancel: false
                })
                return;
            }
            // return;
            let encryptedData = e.detail.encryptedData
            let iv = e.detail.iv
            let code = getApp().globalData.WXCODE
            //console.log(code)  
            wx.request({
                url: that.data.url + 'wxUser/app/login',
                method: 'POST',
                dataType: JSON,
                data: {
                    encryptedData: encryptedData,
                    iv: iv,
                    code: code
                },
                success: function (data) {
                    console.log("请求完成！")
                    //console.log(data)
                    let res1 = JSON.parse(data.data);
                    if (res1.state == 5000) {    
                        wx.showToast({
                            title: '用户不存在，请联系管理员',
                            icon: 'none',
                            duration: 3000
                        })
                    }
                    // 解析成功进行模拟登录
                    if (res1.state == 2000) {
                        that.setData({
                            "loginForm.username": res1.account,
                            "loginForm.password": res1.password,
                        })
                        that.submitForm()
                    }
                },
                fail: function () {
                    console.log('系统错误')
                    wx.showToast({
                        title: '失败',
                    })
                }
            })

            // wx.login({
            //     success: (res) => {
            //         let code = getApp().globalData.WXCODE
            //         wx.request({
            //             url: that.data.url + 'wxUser/app/login',
            //             method: 'POST',
            //             dataType: JSON,
            //             data: {
            //                 encryptedData: encryptedData,
            //                 iv: iv,
            //                 code: code
            //             },
            //             success: function (data) {
            //                 console.log(data)
            //                 let res1 = JSON.parse(data.data);
            //                 if (res1.code == 5000) {
            //                     wx.showToast({
            //                         title: '用户不存在，请联系管理员',
            //                         icon: 'none',
            //                         duration: 3000
            //                     })
            //                 }
            //                 // 解析成功进行模拟登录
            //                 if (res1.state == 2000) {
            //                     that.setData({
            //                         "loginForm.username": res1.account,
            //                         "loginForm.password": res1.password,
            //                     })
            //                     that.submitForm()
            //                 }
            //             },
            //             fail: function () {
            //                 console.log('系统错误')
            //                 wx.showToast({
            //                     title: '失败',
            //                 })
            //             }
            //         })
            //     },
            //     error: (res) => {
            //         console.log('失败');
            //     },
            // })
        },
        //底部获取手机号关闭事件
        close: function () {
            this.setData({
                dialog1: false,
            });
        },
        // 模拟登录
        submitForm: function () {
            var n = this;
            //console.log(n.data.loginForm);
            if(n.data.loginForm.username==undefined || n.data.loginForm.password==undefined) {
                wx.showToast({
                    title: '错误！请重试!',
                    icon: 'none',
                    duration: 3000
                })
                return;
            }
            wx.request({
                url: n.data.url + "login",
                method: "POST",
                data: {
                    username: n.data.loginForm.username,
                    password: n.data.loginForm.password
                },
                success: function (t) {
                    //console.log(t);
                    var o = n.data.loginForm.username;
                    //console.log("请求成功！", t.data.token)
                    wx.request({
                        url: n.data.url + "apiunattended/apiunattended/getUserInfo",
                        method: "GET",
                        data: {
                            username: o
                        },
                        success: function (n) {
                            getApp().globalData.userinfo = n.data;
                            wx.getStorageSync("key");
                            wx.switchTab({
                                url: "/pages/main/index?users=" + o + ''
                            });
                            return;
                            setTimeout((function () {
                                wx.switchTab({
                                    url: "/pages/main/index?users=" + concat(o)
                                })
                            }), 1e3)
                        },
                        fail: function () {
                            console.log("请求失败！")
                        },
                        complete: function () {
                            console.log("模拟登录完成！")
                            // 储存当前登录的cid
                            //console.log("登录之后：", getApp().globalData.WXLOGINCID)

                            let cid = getApp().globalData.WXLOGINCID
                            wx.request({
                                url: n.data.url + "appLoginRedis/setAccountInfo",
                                method: "GET",
                                data: {
                                    equipmentCID: cid,
                                    accountNumber: n.data.loginForm.username,
                                    accountPassword: n.data.loginForm.password
                                }
                            })

                            return;
                            wx.getPushClientId({
                                success: function (n) {
                                    var t = n.cid;
                                    e.request({
                                        url: getApp().globalData.apiurl + "zrwjMessage/setCID",
                                        method: "GET",
                                        data: {
                                            userKEY: getApp().globalData.userinfo.nickName,
                                            messCID: t
                                        }
                                    })
                                },
                                fail: function (e) {
                                    console.log(e)
                                }
                            })
                        }
                    })
                },
                fail: err => {
                    console.log(err)
                },
                complete: function () {

                    console.log("end!!!")
                    // n.insertCID()
                }
            })
        },
    }
})